﻿@page "/{type}/{year}"
@page "/{type}/{year}/{searchKey}"

@using Blog.MVP.Blazor.SSR.Models

@inject HttpClient Http


<div class="container mt-4 mb-5">
    <div class="row">
        <section class="col-lg-9 col-md-12">

            <ArtTitle Title="@Title" Subtitle="@Subtitle" />

            <hr>

            <input type="text" class="form-control mb-2 mr-sm-2 search-bottom-40" placeholder="输入标题关键字，回车" value="@searchKey" @onchange="(e) => { searchKey = e.Value.ToString();SearchArs(); }" />

            @if (_blogs != null)
            {
                foreach (var item in _blogs.response)
                {
                    <ArtBody bID="@item.bID" Num="@(_blogs.response.Count-num)" btitle="@item.btitle" bcontent="@item.bcontent" bCreateTime="@(!string.IsNullOrEmpty(item.bCreateTime) ? DateTime.Parse(item.bCreateTime):DateTime.Now)" btraffic="@item.btraffic" />
                    num++;
                }
            }
            else
            {
                <Loading />
            }

        </section>

        <Author />


    </div>
</div>

@code {
    [Parameter]
    public string searchKey { get; set; } = "";
    [Parameter]
    public string type { get; set; } = "";
    [Parameter]
    public string year { get; set; } = "2020";
    private int num = 0;
    private MessageModel<List<BlogArticleVo>> _blogs;
    private List<BlogArticleVo> _blogs2 = new List<BlogArticleVo>();
    private string Title = "";
    private string Subtitle = "";

    protected override async Task OnParametersSetAsync()
    {
        this._blogs = await Http.GetFromJsonAsync<MessageModel<List<BlogArticleVo>>>("http://apk.neters.club/api/Blog/GetBlogsByTypesForMVP?types=" + type);
        _blogs.response = _blogs.response.OrderByDescending(d => d.bCreateTime).ToList();
        _blogs2 = _blogs.response;
        if (searchKey.IsNotEmptyOrNull())
        {
            var searchArs = _blogs.response.Where(d => d.btitle != null && d.btitle.ToLower().Contains(searchKey.ToLower().Trim())).ToList();
            _blogs.response = searchArs;
        }

        num = 0;
        Title = type.Replace("MVP_", "");
        Subtitle = $"Reader: {_blogs2.Sum(d => d.btraffic).ObjToString()}";
    }

    private void SearchArs(MouseEventArgs e = null)
    {
        num = 0;
        this._blogs.response = _blogs2;

        if (searchKey.IsNotEmptyOrNull())
        {
            var searchArs = _blogs.response.Where(d => d.btitle != null && d.btitle.ToLower().Contains(searchKey.ToLower().Trim())).ToList();
            _blogs.response = searchArs;
        }
    }
}